package app;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.MalformedURLException;
import java.net.URL;

import app.structure.Graph;


public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		URL startPage = null;
		try {
			if(args.length == 5 && args[1].equals("attack")) {
				int attackType = -1;
				attackType = Integer.parseInt(args[2]);
				int offset = 0;
				offset = Integer.parseInt(args[3]);
				String bindIp = args[4]; 
				
				Graph g = null;
				File f1 = new File(args[0]);
				if(f1.exists()) {
						FileInputStream fis = new FileInputStream(f1);
						ObjectInputStream ois = new ObjectInputStream(fis);
						g = (Graph) ois.readObject();
						ois.close();
						fis.close();
				}
				else {
					throw new FileNotFoundException();
				}
				if(g != null) {
					//System.out.println(g.toString() + "\n");
					startPage = new URL(g.getPrefix());
					Attacker att = new Attacker(startPage.getHost(), attackType, offset, g, bindIp);
					att.start();
				}
			}
			else {
				System.out.println("args: graph_path attack attackType[1/2/3] offset(ms)");
			}
		} catch (MalformedURLException e) {
			System.out.println(startPage + " is not a valid URL");
		} catch (NumberFormatException e) {
			System.out.println(args[2] + " is not a valid attackType[1/2/3]");
		} catch (FileNotFoundException e) {
			System.out.println("Couldn't access " + args[0]);
		} catch(ArrayIndexOutOfBoundsException e) {
			System.out.println("args: graph_path attack attackType[1/2/3]");
		} catch (IOException e) {
			System.out.println("File not found " + args[0]);
		} catch (ClassNotFoundException e) {
			System.out.println("Couldn't access " + args[0]);
		}		
	}
}

